package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chencfgs
 * @date 2019-12-08 20:03
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @RequestMapping("/getUsername")
    public Result getUsername() {
        try {
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            if (user != null) {
                String username = user.getUsername();
                System.out.println(username);
                return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, username);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
        return new Result(false, MessageConstant.GET_USERNAME_FAIL, "未获取到名称");
    }

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult;
        try {
            pageResult = userService.findPage(queryPageBean);
        } catch (Exception e) {
            return new Result(false, "查询失败");
        }
        return new Result(true, "查询成功", pageResult);
    }

    //添加用户
    @RequestMapping("/adduser")
    public Result add(@RequestBody com.itheima.pojo.User user,Integer[] roleIds) {
        try {
            userService.add(user,roleIds);
            System.out.println(user);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }

        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //删除
    @RequestMapping("/deleteuser")//CHECKITEM_DELETE
    public Result deleteById(Integer id) {

        try {
            userService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    //编辑
    @RequestMapping("/edituser")
    public Result edit(@RequestBody com.itheima.pojo.User user,Integer[] roleIds) {
        try {
            userService.update(user,roleIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }


    //获取所有的角色信息
    @RequestMapping("/getAllRoles")
    public Result getAllRoles(){
        List<Role> roles =userService.getAllRoles();
        return new Result(true,"查询成功",roles);
    }


    //更改用户密码
    @RequestMapping("/updateUser")
    public Result updateUser(Integer id,String password){
        try {
            Result result = userService.updateUser(id,password);
            return result;
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"更改密码失败");
        }
    }
}
